using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Guru.Utilities.Cryptography
{
    public class CryptoServices : ICryptor
    {
        static CryptoServices _Current = new CryptoServices();
        public static CryptoServices Current
        {
            get
            {
                return _Current;
            }
        }

        public enum Algorithm
        {
            MD5,
            SHA1
        }
        
        public string EncryptText(string str)
        {
            return EncryptText(str, "8ByteDds");
        }
        public string DecryptText(string str)
        {
            return DecryptText(str, "8ByteDds");
        }

        public string EncryptText(string str, string key)
        {
            try
            {
                if (str == null || str == "") return "";
                DES des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.CBC;
                des.Key = Encoding.ASCII.GetBytes(key);
                des.IV = Encoding.ASCII.GetBytes("DDS8BYTE");

                byte[] plainbytes = Encoding.ASCII.GetBytes(str);
                ICryptoTransform transform = des.CreateEncryptor(des.Key, des.IV);
                str = Convert.ToBase64String(transform.TransformFinalBlock(plainbytes, 0, plainbytes.Length));
                return str;
            }
            catch
            {
                return "";
            }
        }

        public string DecryptText(string str, string key)
        {
            try
            {
                if (str == null || str == "") return "";
                DES des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.CBC;
                des.Key = Encoding.ASCII.GetBytes(key);
                des.IV = Encoding.ASCII.GetBytes("DDS8BYTE");

                byte[] cryptedbytes = Convert.FromBase64String(str);

                ICryptoTransform transform = des.CreateDecryptor(des.Key, des.IV);
                str = Encoding.ASCII.GetString(transform.TransformFinalBlock(cryptedbytes, 0, cryptedbytes.Length));
                return str;
            }
            catch
            {
                return "";
            }
        }
        public string MD5(string cleanString)
        {
            return HashText(cleanString, Algorithm.MD5);
        }
        public string SHA1(string cleanString)
        {
            return HashText(cleanString, Algorithm.SHA1);
        }
        private string HashText(string cleanString,Algorithm algo)
        {
            Byte[] clearBytes = new System.Text.UnicodeEncoding().GetBytes(cleanString);
            string algoString = "";
            switch (algo)
            {
                case Algorithm.MD5:
                    algoString = "MD5";
                    break;
                case Algorithm.SHA1:
                    algoString = "SHA1";
                    break;
            }
            Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName(algoString)).ComputeHash(clearBytes);
            return BitConverter.ToString(hashedBytes);
        }
        
        //public static void Main()
        //{
        //    string g = EncryptText("a238894d-6070-4eb7-b3ae-11dee43c951a");
        //    Console.WriteLine(g);
        //    g = DecryptText("8j683ItJi7rJ9zO9E8Lgu/IpC4yWCezulu+g/P9pb+IkmNaKZICzbw==");
        //    Console.WriteLine(g);
        //    Console.ReadKey();
        //}
    }

    public interface ICryptor
    {
        string EncryptText(string str);
        string DecryptText(string str);
        string EncryptText(string str, string key);
        string DecryptText(string str, string key);
    }
}
